// pages/read/read.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    chunkedImages: [],
    tokenImages: [],
    chunkIndex: 0,
    comic_id: null,
    ep_id: null,
    title: '',
    last_comic_id: wx.getStorageSync('history') || [],
    isShow: false,
  },

  showBar() {
    this.setData({
      isShow: !this.data.isShow
    })
  },

  prev() {
    console.log('上一章');
  },

  next() {
    console.log('下一章');
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.comic_title + ' 第 ' + options.ep_title + ' 话',
    })

    this.setData({
      comic_id: options.comic_id,
      ep_id: options.ep_id,
      comic_title: options.comic_title,
      ep_title: options.ep_title,
      history: wx.getStorageSync('history') || ''
    })

    wx.setStorageSync('history', {
      ...this.data.history,
      [options.comic_id]: options.ep_id
    })

    wx.showLoading({
      title: '加载中',
    })

    wx.request({
      url: `https://apis.netstart.cn/mbcomic/GetImageIndex?ep_id=${options.ep_id}`,
      success: (res) => {
        // console.log(res.data.data.images);
        this.setData({
          images: res.data.data.images,
          chunkedImages: new Array(Math.ceil(res.data.data.images.length / 10)).fill(null).map((o, i) => res.data.data.images.slice(i * 10, (i + 1) * 10))
        })

        wx.request({
          url: 'https://apis.netstart.cn/mbcomic/ImageToken',
          data: {
            urls: JSON.stringify(this.data.chunkedImages[this.data.chunkIndex].map(o => o.path + '@660w.jpg'))
          },
          success: (res) => {
            var tokenImages = this.data.tokenImages
            tokenImages[this.data.chunkIndex] = res.data.data
            this.setData({
              tokenImages: tokenImages,
              chunkIndex: this.data.chunkIndex + 1
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.chunkIndex <= (this.data.chunkedImages.length - 1)) {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: 'https://apis.netstart.cn/mbcomic/ImageToken',
        data: {
          urls: JSON.stringify(this.data.chunkedImages[this.data.chunkIndex].map(o => o.path + '@660w.jpg'))
        },
        success: (res) => {
          var tokenImages = this.data.tokenImages
          tokenImages[this.data.chunkIndex] = res.data.data
          this.setData({
            tokenImages: tokenImages,
            chunkIndex: this.data.chunkIndex + 1
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    } else {
      var ep_list = wx.getStorageSync('ep_list') || []
      // if (ep_list) {
        var currentEpIndex = ep_list.findIndex(o => o.id == this.data.ep_id)
        var nextEpIndex
        if (currentEpIndex >= ep_list.length - 1) {
          //已经是最后一章
        } else {
          nextEpIndex = currentEpIndex + 1
          wx.redirectTo({
            url: `/pages/read/read?comic_id=${this.data.comic_id}&ep_id=${ep_list[nextEpIndex].id}&comic_title=${this.data.comic_title}&ep_title=${ep_list[nextEpIndex].short_title}`,
          })
        }
      }
      //  else {
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: 'https://apis.netstart.cn/mbcomic/ComicDetail?',
          data: {
            comic_id: this.data.comic_id
          },
          success: (res) => {
            // console.log(res.data.data);
            var ep_list = res.data.data.ep_list.reverse()
            wx.setStorageSync('ep_list', ep_list)
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      // }
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})